package com.gok.seasonmovie.mapper;

import com.gok.seasonmovie.entity.Cinema;
import com.gok.seasonmovie.entity.Movie;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface CinemaMapper {
    @Select("SELECT " +
            "    c.*, " +
            "    COALESCE(MIN(b.price), 0) AS minprice, " +
            "    (ROUND(6371 * acos(cos(radians(#{latitude})) " +
            "        * cos(radians(c.latitude)) " +
            "        * cos(radians(c.longitude) - radians(#{longitude})) " +
            "        + sin(radians(#{latitude})) " +
            "        * sin(radians(c.latitude))), 2)) AS distance " +
            "FROM " +
            "    cinema c " +
            "LEFT JOIN " +
            "    booking b ON c.id = b.cid " +
            "GROUP BY " +
            "    c.id " +
            "ORDER BY " +
            "    distance ASC")
    List<Cinema> getAllCinema(String longitude, String latitude);
    @Select("select * from cinema WHERE id=#{id}")
    List<Cinema> getCinemaByCid(@Param("id") String id);

    @Select("""
    SELECT 
        c.*, 
        MIN(b.price) AS min_price,
        (ROUND(6371 * acos(cos(radians(#{latitude})) 
             * cos(radians(c.latitude)) 
             * cos(radians(c.longitude) - radians(#{longitude})) 
             + sin(radians(#{latitude})) 
             * sin(radians(c.latitude))), 2)) AS distance 
    FROM 
        cinema c 
        JOIN booking b ON c.id = b.cid 
    WHERE 
        b.mid = #{id} 
    GROUP BY 
        c.id
    ORDER BY 
        distance ASC
""")
    List<Cinema> getCinemaByMid(String id, String longitude,String latitude);
}
